<?php
class PublicationPricesValidator extends sfValidatorBase
{
	protected function configure($options = array(), $messages = array())
	{
	    $this->addRequiredOption("form");
		parent::configure($options, $messages);

		// your validator configuration here
	}

	protected function doClean($values)
	{
	  $errorSchema = new sfValidatorErrorSchema($this);
	  
	  if($this->getOption("form")->getObject()->getPrice()!=$values["price"]){
	  	$errorSchema->addError(new sfValidatorError($this, sprintf("'%s' is changed 'access denied' ",$values["price"])),"price");
	  	throw new sfValidatorErrorSchema($this, $errorSchema);
	  }else{
	  	$totalPrice=$this->getOption("form")->getObject()->getPrice();
	  }
	  
	  if(isset($values[PublicationMergePricesForm::FORM_PRICES])){
	  	foreach($values[PublicationMergePricesForm::FORM_PRICES] as $embedFormKey=>$formValuesPrice){
	  		$currentPrice=0;
	  		$percentageAmount=0;
	  		$percentage= abs($formValuesPrice["percentage"]);
	  		$percentageType=$formValuesPrice["percentage_type"];
	  		$priceCompare=0;
	  		foreach($formValuesPrice[PublicatioPriceMergeCustomizationForm::FORM_CUSTOMIZATIONS] as $keyCustom=>$customDataValues){
	  			$currentPrice+=$customDataValues["amount"];
	  		}
	  		
	  		$percentageAmount=round(($totalPrice/100*$percentage),2);
	  		
	  		if($percentageType==PublicationPriceTable::PERCENTAJE_TYPE_DISCOUNT){
	  			$priceCompare=$totalPrice-$percentageAmount;
	  		}elseif($percentageType==PublicationPriceTable::PERCENTAJE_TYPE_INCREASE){
	  			$priceCompare=$totalPrice+$percentageAmount;
	  		}else{
	  			$priceCompare=$totalPrice;
	  		}
	  		
	  		if($currentPrice!=$priceCompare){
	  			$errorSchema->addError(new sfValidatorError($this, sprintf("la suma de la/las cuotas de del precio <span class='recalc'>'%s'</span> (<span class='recalc'>%s</span>) no es igual al precio total (<span class='recalc'>%s</span>) ",$formValuesPrice["description"],$currentPrice,$priceCompare)),"price");
	  		}
	  	}
	  }
	  
	  
		
	  
	  /*$c= new Criteria();
	  $c->add(MessageUserTagsPeer::ID_USER,$this->getOption("id_user"));
	  $c->add(MessageUserTagsPeer::NAME,$values["name"]);
	  $c->add(MessageUserTagsPeer::MESSAGE_USER_TAG_ID,$values["message_user_tag_id"],Criteria::NOT_EQUAL);
	  
		// Your validator logic here
		if (MessageUserTagsPeer::doCount($c))
		{
			$errorSchema->addError(new sfValidatorError($this, sprintf("'%s' already exist",$values["name"])),"name");//throw new sfException('My exception message');
		}
		if (count($errorSchema))
		{
			throw new sfValidatorErrorSchema($this, $errorSchema);
		}*/
	  if(count($errorSchema)){
	  	throw new sfValidatorErrorSchema($this, $errorSchema);
	  }
		return $values;
	}
}